/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.demo.server.dao.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import org.toools.gwt.demo.jpa.Account;
import org.toools.gwt.demo.server.dao.IAccountDAO;

/**
 *
 * @author ich
 */
public class AcountDAO implements IAccountDAO {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager)
    {
    	em = entityManager;
    }

    public Account getAccountByID(Integer id) {
       Account result;
        try {
            result = (Account) em.createNamedQuery("Account.getById").setParameter("id", id).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        System.out.println("fetched account " + result.getId() + " from db.");
        return result;
    }

    public List<Account> getAccounts() {
        List<Account> resultList = em.createNamedQuery("Account.getAll").getResultList();
        System.out.println("fetched " + resultList.size() + " accounts from db.");
        return resultList;
    }

}
